package in.muton.edushare.components.calendar;

import in.muton.edushare.components.calendar.api.CalendarInterface;
import in.muton.edushare.components.calendar.api.CalendarPanelHelper;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.schedules.widgets.DateHelper;
import in.muton.edushare.wicket.models.CayenneList;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Schedules;

public class CalendarGridView extends EdusharePanel {

	public CalendarGridView(String id,
			IModel<CayenneList<Schedules>> scheduleModel, boolean isAuthorised,
			IModel<Date> currentDate) {
		super(id);
		this.isAuthorised = isAuthorised;
		this.currentDate = currentDate;
		this.schModel = scheduleModel;
		add(new CGridView("rows", getDataProvider()));
		add(JavascriptPackageResource.getHeaderContribution(Event.class,
				"Event.js"));
		add(JavascriptPackageResource.getHeaderContribution(Event.class,
				"EventHider.js"));
	}

	private class CGridView extends GridView<Date> {
		private static final long serialVersionUID = 9146132728099482826L;

		public CGridView(String id, IDataProvider<Date> dataProvider) {
			super(id, dataProvider);
			this.setColumns(7);
		}

		@Override
		protected void populateEmptyItem(Item<Date> arg0) {
			Label l = new Label("date", "");
			arg0.add(l);
			arg0.add(new Label("fullDate",""));
			WebMarkupContainer wmc = new WebMarkupContainer("eventTitle");
			arg0.add(wmc);
			Panel p = new EmptyPanel("eventTooltip");
			wmc.add(p);
			l.add(new AttributeAppender("style", new Model<String>(){
				private static final long serialVersionUID = -641214836320360870L;
				@Override
				public String getObject(){
					return "background-color:#aaaaaa";
				}
			}, ";"));
			wmc.setVisible(false);
			p.setVisible(false);
			arg0.add(new SimpleAttributeModifier("style",
					"background-color:#aaaaaa"));
		}

		@Override
		protected void populateItem(Item<Date> arg0) {
			if (arg0.getIndex() < CalendarPanelHelper
					.getNumberofBlankDays(currentDate.getObject())) {
				populateEmptyItem(arg0);
				return;
			}
			Label l = new Label("date", new SimpleDateFormat("d").format(arg0
					.getModelObject()));
			arg0.add(l);
			arg0.add(new Label("fullDate", new SimpleDateFormat("dd/MM/yyyy")
					.format(arg0.getModelObject())));
			Date today = new DateHelper(new Date()).setHours(0).setMins(0)
					.setSecs(0).getDate();

			if (isAuthorised) {
				if (DateHelper.before(today, arg0.getModelObject())) {
					l.add(new SimpleAttributeModifier("class",
							"newScheduleLink"));
				} else if (DateHelper.sameDay(today, arg0.getModelObject())) {
					l.add(new SimpleAttributeModifier("class",
							"newScheduleLink"));
					l
							.add(new SimpleAttributeModifier("style",
									"color:#ff3333"));
				} else {
					l
							.add(new SimpleAttributeModifier("style",
									"color:#aaaaaa"));
				}
			} else {
				if (DateHelper.sameDay(today, arg0.getModelObject())) {
					l
							.add(new SimpleAttributeModifier("style",
									"color:#ff3333"));
				} else {
					l
							.add(new SimpleAttributeModifier("style",
									"color:#aaaaaa"));
				}
			}
			RepeatingView events = new RepeatingView("eventTitle");
			arg0.add(events);

			List<IModel<Schedules>> lst = schModel.getObject().getObject();
			for (IModel<Schedules> evt : lst) {
				if (CalendarInterface.isCalendarEntryOfDate(arg0
						.getModelObject(), evt.getObject())) {
					WebMarkupContainer wmcEvents = new WebMarkupContainer(
							events.newChildId());
					events.add(wmcEvents);
					wmcEvents.add(new Event("eventTooltip", evt, arg0
							.getModelObject()));
				}
			}

		}
	}

	private IDataProvider<Date> getDataProvider() {
		return new IDataProvider<Date>() {
			private static final long serialVersionUID = 4116494037330517036L;

			public Iterator<? extends Date> iterator(int first, int count) {
				return CalendarPanelHelper.getListOfDays(
						currentDate.getObject()).subList(first, first + count)
						.iterator();
			}

			public IModel<Date> model(Date object) {
				return new Model<Date>(object);
			}

			public int size() {
				return CalendarPanelHelper.getListOfDays(
						currentDate.getObject()).size();
			}

			public void detach() {
			}
		};
	}

	private IModel<CayenneList<Schedules>> schModel = null;
	private IModel<Date> currentDate = null;
	private boolean isAuthorised = false;
	private static final long serialVersionUID = -7602777758031709812L;
}
